package com.business.web.controller;

import com.business.customer.model.auto.CustomerInfo;
import com.business.customer.service.CustomerInfoService;
import com.business.system.common.base.BaseController;
import com.business.weixin.util.Weixin;
import com.business.weixin.util.WeixinService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @ClassName: WechatPageController
 * @author lujingyuan
 * @date 20201123
 */
@Controller
@RequestMapping("/wepage")
public class WechatPageController extends BaseController{

	private String prefix = "wechatPage";
	@Autowired
	private CustomerInfoService customerInfoService;
	@Autowired
	WeixinService weixinService;

	@GetMapping("/addAppealPage")
	public String addAppealPage(HttpServletRequest req,ModelMap modelMap){
		String openid=req.getParameter("openid");
		if(StringUtils.isBlank(openid)){
			String code=req.getParameter("code");
			openid=weixinService.getOpenidByCode(code);
		}
		req.setAttribute("openid",openid);
		modelMap.put("openid",openid);
		return prefix + "/addAppealPage";
	}

	/**
	 * 微信客户注册页面
	 * @param req
	 * @param modelMap
	 * @return
	 */
	@GetMapping("/wechatRegist")
	public String wechatRegist(HttpServletRequest req,ModelMap modelMap){
		String openid=req.getParameter("openid");
		if(StringUtils.isBlank(openid)){
			String code=req.getParameter("code");
			openid=weixinService.getOpenidByCode(code);
		}
		req.setAttribute("openid",openid);
		modelMap.put("openid",openid);
		return prefix + "/wechatRegist";
	}

	@GetMapping("/appealForm")
	public String toAppealForm(HttpServletRequest req,ModelMap modelMap){
		String openid=req.getParameter("openid");
		if(StringUtils.isBlank(openid)){
			String code=req.getParameter("code");
			openid=weixinService.getOpenidByCode(code);
		}
		Map<String,String> map=new HashMap<String,String>();
		map.put("openid",openid);
		List<CustomerInfo> list=customerInfoService.selectByMap(map);
		req.setAttribute("openid",openid);
		modelMap.put("openid",openid);
		if(list!=null&&list.size()>0){
			return prefix + "/appealForm";
		}else{
			return prefix + "/wechatRegist";
		}
	}


	@GetMapping("/testJsSdk")
	public String testJsSdk(ModelMap modelMap){
        String noncestr= Weixin.noncestr;
        String timestamp=weixinService.getTimestamp();
        String url=Weixin.domain+"/wepage/testJsSdk";
		String signature=weixinService.getSignature(url,timestamp,noncestr);
		modelMap.put("appId", Weixin.appId);
		modelMap.put("noncestr", noncestr);
		modelMap.put("timestamp", timestamp);
		modelMap.put("signature", signature);
		modelMap.put("url", url);
		return prefix + "/testJsSdk";
	}

}
